package com.thrd.taiexhibition.fragement;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;

import com.thrd.basicframework.bean.BaseFragment;
import com.thrd.basicframework.ui.defaultview.LoadingState;
import com.thrd.basicframework.ui.recyclerview.RecyclerViewPageChangeListenerHelper;
import com.thrd.taiexhibition.BaseApplication;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.adapter.InvestmentEnvironmentRightAdapter;
import com.thrd.taiexhibition.bean.BeanCityInvestArticle;
import com.thrd.taiexhibition.bean.BeanCityInvestDay;
import com.thrd.taiexhibition.bean.CityEvent;
import com.thrd.taiexhibition.presener.CityInvestmentEnvironmentPresener;
import com.thrd.taiexhibition.ui.LinearTextView;
import com.thrd.taiexhibition.utils.DialogUtils;
import com.thrd.taiexhibition.utils.JumpUtils;
import com.thrd.taiexhibition.view.CityInvestmentEnvironmentView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.List;

/**
 * 城市投资环境
 * @author: ${唐杰}
 * @date: 2021/3/20
 */
public class CityInvestmentEnvironmentFragment extends BaseFragment implements CityInvestmentEnvironmentView {
    private LinearTextView mRecyclerTab;
    private RecyclerView mRecyclerContent;
    private InvestmentEnvironmentRightAdapter adapterRight;
    private CityInvestmentEnvironmentPresener mCityInvestmentEnvironmentPresener;
    private int position=0;
    private int cityId;


    @Override
    protected int setLayoutResourceID() {
        return  R.layout.fagment_investment_environment;
    }

    @Override
    protected void setUpView(View mContentView) {
        mCityInvestmentEnvironmentPresener = new CityInvestmentEnvironmentPresener(this);

        mRecyclerTab = mContentView.findViewById(R.id.ltv_textview);
        mRecyclerContent = mContentView.findViewById(R.id.recycler_content);
        LinearLayoutManager layoutManager=   new LinearLayoutManager(getMContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        SnapHelper snapHelper=new PagerSnapHelper();
        snapHelper.attachToRecyclerView(mRecyclerContent);
        mRecyclerTab.setSelect(position);
        mRecyclerContent.setLayoutManager(layoutManager);
        mRecyclerContent.addOnScrollListener(new RecyclerViewPageChangeListenerHelper(snapHelper,
                new RecyclerViewPageChangeListenerHelper.OnPageChangeListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        // Log.e("Zorro", "选中" + newState);
                    }

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                      //  Log.e("Zorro", "dx" + dx+"dy"+dy);
                    }

                    @Override
                    public void onPageSelected(int position) {
                        CityInvestmentEnvironmentFragment.this.position=position;
                        mRecyclerTab.setSelect(position);

                    }
                }));
        mRecyclerTab.setOnItemClickListener(new LinearTextView.OnItemClickListener() {
            @Override
            public void onItemClickListener(int item) {
                mRecyclerContent.smoothScrollToPosition(item);
            }
        });
    }

    @Override
    protected void setUpData() {

        adapterRight = new InvestmentEnvironmentRightAdapter(getMContext(),null);
        mRecyclerContent.setAdapter(adapterRight);
        adapterRight.setOnInvestmentEnvironmentRight(new InvestmentEnvironmentRightAdapter.OnInvestmentEnvironmentRight() {
            @Override
            public void onQrCodeListener(View view,String path) {
                DialogUtils.showQRDialog(getActivity(),view,path);
            }
        });

        adapterRight.setOnClickCityInvestArticle(new InvestmentEnvironmentRightAdapter.OnClickCityInvestArticle() {
            @Override
            public void OnClickCityInvestArticle(BeanCityInvestArticle t) {
                JumpUtils.JumpUtils(2,t.getArticleId());
            }

            @Override
            public void OnClickExhibition(long exhibition) {
                JumpUtils.JumpUtils(0,exhibition);
            }
        });

    }

    @Override
    public void onError(String error, int type) {
        getContentView().setState(LoadingState.STATE_ERROR);
    }

    private List<BeanCityInvestDay> mBeanCityInvestment;
    @Override
    public void onSuccess(List<BeanCityInvestDay> mBeanCityInvestment) {
        this.mBeanCityInvestment=mBeanCityInvestment;
        adapterRight.setData(mBeanCityInvestment);
        getContentView().setState(LoadingState.STATE_SUCCESS);
    }

    @Override
    protected void onRecoveryBundle(Bundle savedInstanceState) {
        super.onRecoveryBundle(savedInstanceState);
        BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
        if(mBaseApplication!=null&&savedInstanceState!=null) {
            if (mBaseApplication.getCityId()==cityId) {
                mBeanCityInvestment = (List<BeanCityInvestDay>) savedInstanceState.getSerializable("BeanCityInvestment");
                position = savedInstanceState.getInt("position");
            }
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("BeanCityInvestment", (Serializable) mBeanCityInvestment);
        outState.putInt("position", position);

    }


    @Override
    public void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
        if(mCityInvestmentEnvironmentPresener!=null&&mBeanCityInvestment==null&&mBaseApplication!=null){
            mCityInvestmentEnvironmentPresener.getCityInvestmentEnvironmentData(cityId= (int) mBaseApplication.getCityId());
        }else if(mBeanCityInvestment!=null){
            onSuccess(mBeanCityInvestment);
        }

    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBeanCityInvestment=null;
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchCity(CityEvent mCityEvent){
        if(mCityEvent!=null&&mCityInvestmentEnvironmentPresener!=null){
            mBeanCityInvestment=null;
            mCityInvestmentEnvironmentPresener.getCityInvestmentEnvironmentData(cityId= (int) mCityEvent.getCityId());
        }

    }
}
